package com.wise.site;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import com.baidu.mapapi.BMapManager;
import com.wise.site.database.SearKeyDB;
import com.wise.site.protocol.action.ModelAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.result.ModelResult;
import com.wise.site.push.PushService;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.FileCache;
import com.wise.site.utils.ImageLoader;
import com.wise.site.utils.Util;

import android.app.Application;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.os.Handler;
import android.preference.PreferenceManager;

/**
 * 应用开启的入口application， 需要加载apk的动态模块
 * 
 * @author huangyue
 * 
 *         2012-9-15
 */
public class WiseSiteApplication extends Application {
	public SharedPreferences mPreference;
	public Handler mHandler;
	private static WiseSiteApplication instance;
	private ModelResult mModelResult;
	private ProgressDialog mProgressDialog;
	private BMapManager mBMapMan;

	private Hashtable<String, DataSetObserver> modelLoad;
	public static boolean isStart = false;

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		mHandler = new Handler();
		mPreference = PreferenceManager.getDefaultSharedPreferences(this);
		modelLoad = new Hashtable<String, DataSetObserver>();
		SearKeyDB.initDB(this);
		ImageLoader.init(this, 0, Util.getExternDir("/wise/img/"));
		FileCache.getInstance().initCache();
		ImageLoader.getInstance().clearFile(2);
		loadModelInfo();
		mBMapMan = new BMapManager(this);
		mBMapMan.init(Constanct.MAP_KEY, null);
		
		isStart = true;
		startService(new Intent(this, PushService.class));
	}

	public void resumeMapMan() {
		if (mBMapMan != null) {
			mBMapMan.start();
		}
	}

	public void pauseMapMan() {
		if (mBMapMan != null) {
			mBMapMan.stop();
		}
	}

	public void finishMapMan() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
		}
	}

	public BMapManager getBMapMan() {
		return mBMapMan;
	}

	public void setOnModelReady(String className, DataSetObserver Observer) {
		modelLoad.put(className, Observer);
	}

	public ModelResult getModelResult() {
		return mModelResult;
	}

	// 异步加载页面初始化数据
	private void loadModelInfo() {
		ModelAction modelAction = new ModelAction(ACTION_TYPE.ACTION_COMMON,
				"gettype");
		modelAction.setActionListener(new ActionListener<ModelResult>() {
			@Override
			public void onSucceed(ModelResult result) {
				mModelResult = result;
				if (!modelLoad.isEmpty()) {
					Iterator<DataSetObserver> observers = modelLoad.values()
							.iterator();
					while (observers.hasNext()) {
						observers.next().onChanged();
					}
				}
			}

			@Override
			public void onError(int resultCode) {
				ModelResult result = (ModelResult) FileCache.getInstance().get(ModelAction.CACHE_KEY);
				if(result != null){
					onSucceed(result);
				}
			}
		});
		ProtocolManager.getProtocolManager().submitAction(modelAction);
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		instance = null;
	}

	/**
	 * 获得主线程句柄 huangyue 2012-9-17
	 */
	public Handler getHandler() {
		return mHandler;
	}

	public static WiseSiteApplication getContext() {
		return instance;
	}
}
